﻿using System.Collections.Generic;

namespace lwzlz.Application.System.Authority
{
    /// <summary>
    /// 
    /// </summary>
    public class SpecialAuthorityHelper
    {
        private SpecialAuthority _specialAuthority;


        public SpecialAuthorityHelper()
        {
            _specialAuthority = SpecialAuthority.GetInstance();
        }

        /// <summary>
        /// 判断用户是否有用户特殊权限
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <returns></returns>
        public bool IsHaveUserSpecialAuthority(int userId)
        {
            foreach (var item in _specialAuthority.UserSpecialAuthority)
            {
                if (item.Value == userId)
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 判断部门是否有部门特殊权限
        /// </summary>
        /// <param name="dptId"></param>
        /// <returns></returns>
        public bool IsHaveDptSpecialAuthority(int dptId)
        {
            foreach (var item in _specialAuthority.DptSpecialAuthority)
            {
                if (item.Value == dptId)
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 判断角色是否有角色特殊权限
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public bool IsHaveRoleSpecialAuthority(int roleId)
        {
            foreach (var item in _specialAuthority.RoleSpecialAuthority)
            {
                if (item.Value == roleId)
                {
                    return true;
                }
            }
            return false;
        }
    }

    /// <summary>
    /// 特殊权限单例，同一时间只允许存在一个特殊权限的实例
    /// </summary>
    public class SpecialAuthority
    {
        // 立即加载，确保访问时已存在实例
        // XXX: 改为懒加载，双重检测防止并发冲突
        private static SpecialAuthority _uniqueInstance = new SpecialAuthority();

        private IDictionary<string, int> _userSpecialAuthority;
        private IDictionary<string, int> _dptSpecialAuthority;
        private IDictionary<string, int> _roleSpecialAuthority;

        /// <summary>
        /// 用户特殊权限字典
        /// key: 权限名; value: 用户ID
        /// </summary>
        public IDictionary<string, int> UserSpecialAuthority { get { return _userSpecialAuthority; } }
        /// <summary>
        /// 部门特殊权限字典
        /// key: 权限名; value: 部门ID
        /// </summary>
        public IDictionary<string, int> DptSpecialAuthority { get { return _dptSpecialAuthority; } }
        /// <summary>
        /// 角色特殊权限字典
        /// key: 权限名; value: 角色ID
        /// </summary>
        public IDictionary<string, int> RoleSpecialAuthority { get { return _roleSpecialAuthority; } }

        private SpecialAuthority()
        {
            // XXX: 临时权限，暂用待改

            // TODO: 限制外部对字典项改动；修改时加锁

            //用户（key：权限名称，value：用户ID）
            _userSpecialAuthority = new Dictionary<string, int>()
            {
                { "OFFICESUPPLIESAPPLY_ALLSEE",680}//办公用品申领_能看所有,单莉莉ID
            };

            //部门（key：权限名称，value：部门ID）
            _dptSpecialAuthority = new Dictionary<string, int>()
            {
                { "OFFICESUPPLIESSEARCH_ALLSEE", 46 }, //办公用品查询_能看所有,行政部ID
            };

            //部门（key：权限名称，value：角色ID）
            _roleSpecialAuthority = new Dictionary<string, int>()
            {
            };
        }

        public static SpecialAuthority GetInstance()
        {
            return _uniqueInstance;
        }

    }
}
